// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.web.browser.dom;

import java.util.HashMap;
import java.util.Map;

import org.dengues.ui.i18n.Messages;
import org.eclipse.swt.internal.ole.win32.COM;
import org.eclipse.swt.internal.ole.win32.IDispatch;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.Variant;

/**
 * @author jliu
 */
public class ExtendedDispatch extends IDispatch {

    private Map<String, int[]> idsCache;

    /**
     * Extended class of org.eclipse.swt.internal.ole.win32.IDispatch, we can use this class to get properties like
     * Variant, IDispatch, String, Integer, and Boolean by a string name easily.
     * 
     * @param address
     */
    public ExtendedDispatch(int address) {
        super(address);
        AddRef();
        idsCache = new HashMap<String, int[]>();
    }

    /**
     * Get Ids of the name from this OleAutomation value.
     * 
     * @param name
     * @param automation
     * @return a array of ids for the name value.
     */
    protected int[] getIDsOfName(String name, OleAutomation automation) {
        int idsOfName[] = (int[]) idsCache.get(name);
        if (idsOfName == null) {
            idsOfName = automation.getIDsOfNames(new String[] { name });
            if (idsOfName != null) {
                idsCache.put(name, idsOfName);
            }
        }
        if (idsOfName == null || idsOfName.length == 0) {
            throw new RuntimeException((new StringBuilder("Property '")).append(name).append("' not found in class").toString()); //$NON-NLS-1$ //$NON-NLS-2$
        } else {
            return idsOfName;
        }
    }

    /**
     * This method will call to check if the property value(by name) is existing for current IDispatch.
     */
    public boolean containsProperty(String name) {
        int dsOfNames[] = (int[]) idsCache.get(name);
        if (dsOfNames == null) {
            Variant variant = new Variant(this);
            OleAutomation automation = variant.getAutomation();
            dsOfNames = automation.getIDsOfNames(new String[] { name });
            automation.dispose();
            if (dsOfNames != null) {
                idsCache.put(name, dsOfNames);
            }
        }
        return dsOfNames != null && dsOfNames.length > 0;
    }

    /**
     * Get a string property value from this IDispatch.
     * 
     * @param name
     * @return the string value of this name.
     */
    public String getStringProperty(String name) {
        Variant property = getVariantProperty(name);
        if (property.getType() == COM.VT_BSTR) {
            return property.getString();
        }
        return "null"; //$NON-NLS-1$
    }

    /**
     * Get a int property value from this IDispatch.
     * 
     * @param name
     * @return the int value of this name.
     */
    public int getIntProperty(String name) {
        Variant property = getVariantProperty(name);
        if (property.getType() == COM.VT_I4) {
            return property.getInt();
        }
        return -1;
    }

    /**
     * Get a Variant property value from this IDispatch.
     * 
     * @param name
     * @return the variant value of this name.
     */
    private Variant getVariantProperty(String name) {
        Variant variant = new Variant(this);
        OleAutomation automation = variant.getAutomation();
        Variant property = automation.getProperty(getIDsOfName(name, automation)[0]);
        automation.dispose();
        return property;
    }

    /**
     * Get a IDispatch property value from this IDispatch.
     * 
     * @param name
     * @return the IDispatch value of this name.
     */
    public IDispatch getDispatchProperty(String name) {
        Variant property = getVariantProperty(name);
        if (property == null || property.getType() == COM.VT_EMPTY || property.getType() == COM.VT_NULL) {
            return null;
        }
        try {
            return property.getDispatch();
        } catch (RuntimeException e) {
        }
        return null;
    }

    /**
     * @param name
     * @param index
     * @return a IDispatch value by using this name and index.
     */
    protected IDispatch getIndexedDispProperty(String name, int index) {
        Variant property = getVariantIndexedDispProperty(name, index);
        if (property == null || property.getType() == COM.VT_EMPTY || property.getType() == COM.VT_NULL) {
            return null;
        } else {
            return property.getDispatch();
        }
    }

    /**
     * @param name
     * @param index
     * @return a variant value by using this name and index
     */
    private Variant getVariantIndexedDispProperty(String name, int index) {
        Variant variant = new Variant(this);
        OleAutomation automation = variant.getAutomation();
        Variant property = automation.invoke(getIDsOfName(name, automation)[0], new Variant[] { new Variant(index) });
        automation.dispose();
        return property;
    }
}
